import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/extension/text_editing_controller.dart';
import 'package:flutter_soundmagus/widget/alert/alert.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:get/get.dart';

part 'cross_pass_controller.dart';

class CrossController extends GetxController with MsgParserRefreshMixin {
  XoverModel get model => DataMng().xoverModel;

  ///选中通道
  final selectCh = DataMng().xoverModel.chConfig.selectCh.obs;

  ///组联调状态
  final groupSelects = List.generate(4, (index) => false.obs);

  ///通道选中状态
  final chSelects = List.generate(DataMng().xoverModel.chConfig.chs.length, (index) => false.obs);

  ///高通、低通、全通
  final passType = PassType.high.obs;
  final passControllers = List.generate(2, (i) {
    var itemModel = DataMng().xoverModel.itemModel();
    var m = i == 0 ? itemModel.high : itemModel.low;
    var c = CrossPassController()
      ..isHigh = i == 0
      ..freq.value = m.freq
      ..slope.value = m.slope
      ..xielv.value = m.type;
    return c;
  });
  CrossController() {
    observeRefresh();

    _init();
    refreshData(false);

    ever(selectCh, (v) {
      refreshData(false);
    });
  }

  _init() {
    selectCh.value = model.selectCH;
    for (var e in model.chConfig.chs) {
      groupSelects[e.groupIndex].value = e.isGroup;
    }
  }

  @override
  refreshData(bool needSendData) {
    super.refreshData(needSendData);
    _init();
    for (var i = 0; i < passControllers.length; i++) {
      var itemModel = DataMng().xoverModel.itemModel();
      var m = i == 0 ? itemModel.high : itemModel.low;
      var c = passControllers[i];
      c
        ..isHigh = i == 0
        ..freq.value = m.freq
        ..slope.value = m.slope
        ..xielv.value = m.type
        ..update();
    }

    //同步联调数据
    selectCh.value = model.selectCH;
    for (var e in model.chConfig.chs) {
      groupSelects[e.groupIndex].value = e.isGroup;
    }
    //同步选中状态
    for (var i = 0; i < model.chConfig.chs.length; i++) {
      chSelects[i].value = model.chConfig.selecteds()[i];
    }

    if (needSendData) {
      Msg.msgXoverSendAllChsData();
    }
  }

  //刷新UI
  @override
  refreshState(bool needSendData) {
    super.refreshState(needSendData);
    refresh();
  }

  ///激活选择通道[ch]点击通道
  void setChSelected({required int ch}) {
    model.chConfig.selectCh = ch;
    selectCh.value = ch;

    for (var i = 0; i < model.chConfig.chs.length; i++) {
      //同步选中状态
      chSelects[i].value = model.chConfig.selecteds()[i];
    }
  }

  ///设置组联调[groupIndex]组索引
  void setGroup({required int groupIndex}) {
    Get.dialog(AlertSelectChannel(
      groupIndex: groupIndex,
      chConfig: model.chConfig,
      onPressed: (i) {
        model
          ..chConfig.selectCh = i
          ..chConfig.setGroup(
            groupIndex: groupIndex,
            relativeCH: i,
          )
          ..syncData();
        selectCh.value = i;
        groupSelects[groupIndex].value = true;

        for (var e in model.chConfig.chs) {
          chSelects[e.index].value = e.groupIndex == groupIndex;
        }

        //下发一次数据
        Msg.msgXoverSendSingleChAllData(ch: i);
      },
    ));
  }

  ///取消组联调
  void cancelGroup({required int groupIndex}) {
    model.chConfig.cancelGroup(groupIndex: groupIndex);
    groupSelects[groupIndex].value = false;

    var chs = model.chConfig.chs.where((element) => element.groupIndex == groupIndex).toList();
    chSelects[chs.last.index].value = false; //组的第二个通道取消高亮
  }

  ///是否通道已选中
  bool isChSelected({required int ch}) {
    return model.chConfig.isChSelected(ch: ch);
  }

  ///取得通道
  Ch getCh({int? ch}) {
    var i = ch ?? model.selectCH;
    return model.chConfig.chs[i];
  }
}
